<?php
final class File{
	
	//获取文件夹下的文件
	static function getImages($path){		 
		$fns = scandir( $path);
		$w=DIR.$path ;
		$files = array();
		foreach($fns as $k=>$v){
			$hz=strstr($v,".");			
			if(
	       		$hz==".gif" or $hz==".jpg" or $hz==".JPG" or $hz=='.jpeg' or
	       		$hz==".JPEG"or $hz==".PNG"or $hz==".png"or $hz==".GIF"
	       	){	       		
	       		$files[] =array(
	       			'id'=>$k,
	       			'url'=>$w.$v
	       		) ; 	       		
	       	}
		}		
        return $files;
	}
	
	
	static function P($str){
		echo '<div>'.$str.'<div>';
	}
	
	//获取文件大小
	static function GetFileSize($path,$b=true){
		$byte = filesize($path);
		if($b){
			return $byte;
		}
	
	    $KB = 1024;
	    $MB = 1024 * $KB;
	    $GB = 1024 * $MB;
	    $TB = 1024 * $GB;
	    
	    if ($byte < $KB) {
	        return $byte . "B";
	    } elseif ($byte < $MB) {
	        return round($byte / $KB, 3) . "KB";
	    } elseif ($byte < $GB) {
	        return round($byte / $MB, 3) . "MB";
	    } elseif ($byte < $TB) {
	        return round($byte / $GB, 3) . "GB";
	    } else {
	        return round($byte / $TB, 3) . "TB";
	    }
		return $size;
	}
	
	//删除文件小于预定义大小的文件 默认75kb
	static function DeleteFileWhereMin($path,$min=76800,$max=999999999999){
		$size=self::getFileSize($path);
		if($size<76800){
			unlink($path) or die('删除文件失败'.DIR.$path);
		}else{
			return true;
		}
	}
	
	//删除一个路径下的所有内容
	static function ClearPath($path){
		//self::p('文件夹路径'.$path);
		if(is_dir($path)){
			$pArr=scandir($path);
			//var_dump($pArr);
			foreach($pArr as $v){
				if($v!='.' && $v!='..'){
					$path2=$path.$v;
					if(is_dir($path2)){
						$path2=$path2.'/';
						self::P('进入目录删除'.$path2);
						self::ClearPath($path2);
						rmdir($path2)or self::P('删除空文件夹 '.$path2.' 失败');
					}else{
						unlink($path2)or self::P('删除文件 '.$path2.' 失败');
						//self::P('删除文件 '.$path.$v.' 成功');
					}
				}				
			}
		}else{
			echo $path.' 不是目录';
		}
	}
	
	//视频上传
	public function getvideo($videoName){
		$video=$_FILES[$videoName];
		$videosize=$video['size'];
		$videotype=$video['type'];
		$videotmpname=$video['tmp_name'];
		$videoerror=$video['error'];
		$videoname=$video['name'];
		$v=$this->savefile($videoerror,$videoname,$videotype,$videotmpname,$videosize);
		return $v;
	}
	
	
	//多视频上传
	function getvideos($videosName){
		$vs='';
		$video=$_FILES[$videosName];
		//求出视频个数
		$len=count( $video['name'] );
		for($i=0;$i<$len;++$i)
		{
			$videosize=$video['size'][$i];
			$videotype=$video['type'][$i];
			$videotmpname=$video['tmp_name'][$i];
			$videoerror=$video['error'][$i];
			$videoname=$video['name'][$i];
			
			$tmppf=$this->savefile($videoerror,$videoname,$videotype,$videotmpname,$videosize);
			if($i<$len-1){
				$vs.= $tmppf .';';
			}else{
				$vs.= $tmppf;
			}
		}
		return $vs;
	}
	
	
	//保存文件
	protected function savefile($videoerror,$videoname,$videotype,$videotmpname,$videosize){
		if($videoerror!=0){
			die(json_encode("错误代码:{$videoerror}")  );
		}
		
		if($videosize<=1){
			die(json_encode("文件太小:".$videosize) );
		}
		
		if(	 $videotype == "video/mp4"   || true   ){
			$videohouzui=strrchr($videoname,'.');
			$v=UPLOAD.time().'_'.rand(0,10000).$videohouzui;
			move_uploaded_file($videotmpname,$v)or die( json_encode('移动文件失败') );
			$url=DIR.$v;
			return $url;
		}else{
			unlink($videotmpname);
			die(json_encode("类型错误:".$videotype) );
		}
		
	}
	
	//删除本服务器图片
	public function delete($videoWebUrl){
		$pf=strrchr($videoWebUrl,UPLOAD);
		unlink($pf)or die('失败');
		return '成功';
	}
}
?>